package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import com.pms.entity.Task;


import com.pms.util.DBHelp;
import com.pms.util.RowMapper;

public class TaskDao {
	
	private DBHelp<Task> db = new DBHelp<Task>();
	 
	public void save(String id,String taskname,String tasktext,String start,String end,String level,String status,int rate,String person,String t_goal_id,String t_user_id){
		String sql="insert into t_task(id,taskname,tasktext,start,end,level,status,rate,person,t_goal_id,t_user_id) value(?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, id,taskname,tasktext,start,end,level,status,rate,person,t_goal_id,t_user_id);
	}
	  
	public List<Task> findAll(String t_goal_id){
	
		String sql="select id,taskname,tasktext,start,end,level,status,rate,person from t_task where t_goal_id = ?";
		return db.executeQueryForList(sql, new TaskRowMapper(),t_goal_id);
	
	}
	public Task findById(String id){
		String sql="select id,taskname,tasktext,start,end,level,status,rate,person from t_task where id=?";
		return db.executeQueryForObject(sql, new TaskRowMapper(),id);

	}
	public void update(Task task){
		String sql = "update t_task set taskname = ?,tasktext = ?,start = ?, end = ?,rate = ?, status = ?,level = ?,person = ? where id = ?";
		db.executeSQL(sql,task.getTaskname(),task.getTasktext(),task.getStart(),task.getEnd(),task.getRate(),task.getStatus(),task.getLevel(),task.getPerson(),task.getId());
	}
	public List<Task> find(){
		String sql = "select id,taskname,tasktext,start,end,level,status,rate,person from t_task";
		return db.executeQueryForList(sql, new TaskRowMapper());
	}
	public void del(String id){
		String sql = "delete from t_task where id = ?";
		db.executeSQL(sql, id);
	}
	public List findrate(String t_goal_id){
		String sql = "select rate from t_task where t_goal_id = ?";
		return db.executeQueryForList(sql, new TaskRowMapper(), t_goal_id);
	}
	
	private class TaskRowMapper implements RowMapper<Task> {
		public Task  mapperRow(ResultSet rs) throws SQLException {
			Task task = new Task();
			task.setId(rs.getString("id"));
			task.setTaskname(rs.getString("taskname"));
			task.setTasktext(rs.getString("tasktext"));
			task.setStart(rs.getString("start"));
			task.setEnd(rs.getString("end"));
			task.setLevel(rs.getString("level"));
			task.setStatus(rs.getString("status"));
			task.setRate(rs.getInt("rate"));
			task.setPerson(rs.getString("person"));
			return task;
		}
	}
}
